package com.bw.controller;

import com.bw.domain.Car;
import com.bw.domain.MonthlyCard;
import com.bw.domain.PlateType;
import com.bw.service.CarService;
import com.bw.utils.MyEx;
import com.bw.utils.R;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.UUID;

/**
 * @author bobo棒
 * @date 2025年02月25日 11:28
 */

@Controller
@RequestMapping("car")
public class CarController {
    @Autowired
    CarService carService;

    /**
     * 批量删除
     * @param ids
     * @return
     */
    @ResponseBody
    @RequestMapping("batchDel")
    public R batchDel(String ids) {
     int i=   carService.batchDel(ids);
     return i>0?R.OK():R.ERROR(500,"删除失败");
    }

    /**
     * 根据月卡卡号获取月卡
     * @param cardNumber
     * @return
     */
    @ResponseBody
    @RequestMapping("checkMontylyCard")
    public R checkMontylyCard(String cardNumber) {
       MonthlyCard monthlyCard = carService.getMontyCard(cardNumber);
        return R.OK(monthlyCard);
    }


    /**
     * 校验车牌是否存在
     * @param plateNumber
     * @return
     */
    @ResponseBody
    @RequestMapping("checkPlateNumber")
    public R checkPlateNumber(String plateNumber) {
      int i=  carService.checkPlateNumber(plateNumber);
      return i>0?R.ERROR(500,"车牌号已存在"):R.OK();
    }

    /**
     * 跳转到添加车辆页面
     *
     * @return
     */
    @RequestMapping("toadd")
    public String toadd() {
        return "/car/add";
    }

    /**
     * 执行添加车辆
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("add")
    public R add(Car car, MonthlyCard monthlyCard) throws MyEx {
        car.setParkNum(UUID.randomUUID().toString());
        boolean b = carService.add(car, monthlyCard);
        return b ? R.OK() : R.ERROR(500, "添加失败");
    }


    /**
     * 获取所有的车牌类型
     *
     * @return
     */
    @ResponseBody
    @RequestMapping("listPlateType")
    public R listPlateType() {
        List<PlateType> plateTypeList = carService.listPlateType();
        return R.OK(plateTypeList);
    }

    /**
     * 返回汽车列表数据
     *
     * @param car
     * @param page
     * @param limit
     * @return
     */
    @ResponseBody
    @RequestMapping("list")
    public R list(Car car, Integer page, Integer limit) {
        PageHelper.startPage(page, limit);
        List<Car> carList = carService.list(car);
        PageInfo<Car> info = new PageInfo<>(carList);
        return R.OK(info);

    }

    /**
     * 进入车辆列表
     *
     * @return
     */
    @RequestMapping("tolist")
    public String tolist() {

        return "/car/list";
    }
}
